package com.clr.springboot.contorller;


import com.clr.springboot.entity.User;
import com.clr.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 车礼荣
 * @version 1.0
 * Create by 2025/3/19 10:25
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //查询单个用户
    @GetMapping("/getUser/{id}")
    @ResponseBody
    public User selectById(@PathVariable("id") Integer id){
        User user=userService.selectByid(id);
        if(user!=null)
            return user;
        else
            return null;
    }
    @PostMapping("/addUser")
    public String addUer(User user){
        int rows=userService.addUser(user);
        if (rows!=0){
            return "成功增加了"+rows+"行";
        }
        else
            return null;
    }

    @PutMapping("/updateUser")
    public String updateUser(User user){
        int rows=userService.updateUser(user);
        if (rows!=0){
            return "成功更新了"+rows+"行";
        }
        else
            return null;
    }
    @DeleteMapping("/deleteUser")
    public String deleteUserById(Integer id){
        int rows=userService.deleteUserById(id);
        if (rows!=0){
            return "成功删除了"+rows+"行";
        }
        else
            return null;
    }
    @GetMapping("/selectall")
    public List<User> selectallUsers(User user) {
        List<User> userList = userService.selectallUsers(user);
        System.out.println("用户数量：" + userList.size());
        return userList;

    }

}